#-*-Makefile-*- vim:syntax=make

### ---
### --- TinyOS Make System Version 3
### ---

# @author Cory Sharp <cssharp@eecs.berkeley.edu>
# @author Brad Campbell <bradjc@umich.edu>

### --- This makefile requires GNU Make version 3.80 or newer.

# Make the coloring work on Mac
SHELL=/bin/bash

### ---
### --- Prepare variables
### ---

#  Most variables are set in Makefile.include

#  Mung MAKERULES for Cygwin; see the warning below for more details.
ifneq ($(findstring \,$(TINYOS_MAKERULES)),)
TINYOS_MAKERULES := $(subst \,/,$(TINYOS_MAKERULES))
define BACKSLASH_WARNING
$(WARN_STRING) TINYOS_MAKERULES contains backslashes.

    The environment variable TINYOS_MAKERULES contains backslashes \'s.  This
    can cause shell scripts including ones in this make system to fail in
    strange ways.  I've changed those to forward slashes for you for this
    build.  However, you are strongly encouraged to respecify TINYOS_MAKERULES
    as either a standard unix-style path or as a mixed-style path where the
    backslashes are replaced with forward slashes /'s.

endef
$(warning $(BACKSLASH_WARNING))
endif

ifndef TINYOS_NO_DEPRECATION_WARNING
define MAKE_VERSION_TWO_WARNING
$(WARN_STRING) Old TinyOS environment variables detected.

    This version of the TinyOS make system has deprecated the TOSDIR, TOSROOT,
    and MAKERULES environment variables. Generally, you shouldn't need
    environment variables with this version of the TinyOS make system, and if
    you are not using any older versions of TinyOS on your system you should
    delete these environment variables. If you wish to leave these variables
    but hide this warning, set the TINYOS_NO_DEPRECATION_WARNING environment
    variable. If you are looking to compile against a different TinyOS tree,
    configure the makefiles used when compiling, or setup an external TinyOS
    tree with additional source files, please visit
    https://github.com/tinyos/tinyos-main/blob/master/support/make/README.md
    for more information about the TinyOS make system.

endef
ifdef TOSROOT
$(warning $(MAKE_VERSION_TWO_WARNING))
else
ifdef TOSDIR
$(warning $(MAKE_VERSION_TWO_WARNING))
else
ifdef MAKERULES
$(warning $(MAKE_VERSION_TWO_WARNING))
endif
endif
endif
endif

#  Allow users to specify additional directories to find .target, .extra, and
#  .rules files. This allows for platforms to be stored in separate repositories
#  and still compile against the main TinyOS code.
#
#  To use this feature, set the environment variable TINYOS_ROOT_DIR_ADDITIONAL
#  to a space or colon separated list of additional paths to make folders you would
#  like the make system to search.
#
#  Make expects space seperated lists, but ':' seperated is easier to read.  We
#  convert ':' to ' ' below so Make is happy.
#
ROOT_DIR_ADDITIONAL = $(subst :, ,$(TINYOS_ROOT_DIR_ADDITIONAL))
TOSMAKE_PATH = $(addsuffix /support/make,$(ROOT_DIR_ADDITIONAL))
TOSMAKE_PATH += $(TINYOS_MAKE_DIR)

#  Also determine the path for all TinyOS OS directories that may have code in
#  them. This makes out-of-tree builds and multiple TinyOS code trees very easy
#  to manage. All relevant folders will automatically be included to the call to
#  nescc.
TOSMAKE_OS_DIR_ALL = $(addsuffix /tos,$(ROOT_DIR_ADDITIONAL)) $(TINYOS_OS_DIR)

# Create identifying flags for the particular application, user, timestamp, etc.
IDENT_FLAGS := $(shell tos-ident-flags "$(COMPONENT)")
CFLAGS += $(IDENT_FLAGS)

#  Set the rules that expand PFLAGS (which have %T in them) to the PFLAGS that
#  nescc sees when it compiles.
TOSDIR_OPTS = $(addprefix --tosdir ,$(TOSMAKE_OS_DIR_ALL))
ADD_SIM_DIRS = $(if $(filter sim sim-fast sim-sf,$(GOALS)),--sim,)
NESC_PFLAGS = $(shell tos-sub-tosdir $(TOSDIR_OPTS) $(ADD_SIM_DIRS) $(PFLAGS) $(TOSMAKE_PFLAGS_STD_INCLUDE))

# Add the defaults to the list of PFLAGS. These are set in Makedefaults.
# - What gcc to use
# - nescc defaults (all warnings, include tos.h)
# - nescc scheduler
# - .c file for nescc output
# - Separator for nescc output
# - Define the default TOS AM group
PFLAGS += -gcc=$(GCC)
PFLAGS += $(NESC_FLAGS)
PFLAGS += -fnesc-scheduler=$(NESC_SCHEDULER)
PFLAGS += -fnesc-cfile=$(NESC_CFILE)
PFLAGS += -fnesc-separator=$(NESC_SEPARATOR)

# Check for Cygwin
OSNAME := $(shell uname -s)
ifeq ($(OSNAME),Cygwin)
  TOSMAKE_CYGWIN := 1
  PFLAGS += -mingw-gcc
endif

# Setup the flags needed to do the wiring check
WIRING_CHECK_FLAGS = -fnesc-dump=wiring -fnesc-dump='interfaces(!abstract())' -fnesc-dump='referenced(interfacedefs, components)' -fnesc-dumpfile=$(WIRING_CHECK_FILE)
WIRING_CHECK_FILE = $(TOSMAKE_BUILD_DIR)/wiring-check.xml

# Set default build targets
BUILD_DEPS ?= tinyos_image

#  Save makecmdgoals (a read only var) to goals so that we can modify it.
GOALS += $(MAKECMDGOALS)

#  Extract user options from goals of the form opt,arg, transform to opt=arg,
#  and evaluate.  Then, reduce GOALS to have the args removed.
OptRE := [,.]
GoalOpts := $(shell perl -e 'print join " ", map {s{^(.*?)$(OptRE)}{\U$$1=};$$_} grep /$(OptRE)/, split /\s+/, "$(GOALS)";')
GOALS := $(shell perl -e '$$_="$(GOALS)"; s{$(OptRE)\S*}{}g; print;')
$(foreach opt,$(GoalOpts),$(eval $(opt)))


# Colors
ifndef TOSMAKE_NO_COLOR
NO_COLOR=\x1b[0m
OK_COLOR=\x1b[32;01m
ERROR_COLOR=\x1b[31;01m
WARN_COLOR=\x1b[33;01m
INFO_COLOR=\x1b[34;01m
endif

OK_STRING=$(OK_COLOR)[OK]$(NO_COLOR)
ERROR_STRING=$(ERROR_COLOR)[ERROR]$(NO_COLOR)
WARN_STRING=$(WARN_COLOR)[WARNING]$(NO_COLOR)
INFO_STRING=$(INFO_COLOR)[INFO]$(NO_COLOR)

### --- Check if a new enough version of tinyos-tools is installed. This really
### --- equates to whether or not the `tos-sub-tosdir` script exists.

define OLD_TOOLS_ERROR

$(ERROR_STRING) Your version of tinyos-tools is too old (<2.2.0).
Please build and install the tinyos-tools in the tinyos-main/tools directory
endef

TOS_SUB_TOSDIR_OUTPUT := $(shell tos-sub-tosdir --help 2>/dev/null)
ifndef TOS_SUB_TOSDIR_OUTPUT
    $(error $(shell echo -e '$(OLD_TOOLS_ERROR)'))
endif

### ---
### --- Define make functions.
### ---
### --- The functions a user will generally be interested in are
### ---   TOSMake_include(file)
### ---   TOSMake_include_platform(dir)
### ---

#  names(words)
#    Produce option names, like junk from /path/to/junk.target.
names = $(sort $(basename $(notdir $(1))))

#  TOSMake_find(file_or_dir)
#    Search for file_or_dir within TOSMAKE_PATH.  For the special case of
#    initializing TOSMAKE_PATH itself, this function does not search
#    TOSMAKE_PATH if file_or_dir begins with +.
sh_search = for a in $(TOSMAKE_PATH); do [ -e "$$a/$$n" ] && echo "$$a/$$n" && break; done
TOSMake_find = $(if $(filter +%,$(1)),$(1:+%=%),$(shell n="$(1)"; $(sh_search)))

#  TOSMake_makelist(dir,extension)
#    Get a list of files with the given extension from a directory which MUST
#    be a subdir under TOSMAKE_PATH.
TOSMake_makelist = $(wildcard $(call TOSMake_find,$(1))/*.$(2))

#  TOSMake_include(file)
#    Include a makefile which MUST be in a dir or subdir under TOSMAKE_PATH.
TOSMake_include = $(eval $(call TOSMake_include_define,$(1)))
define TOSMake_include_define
$(if $(call TOSMake_find,$(1)),,$(error File $(1) not found in any TinyOS make path. The search path is '$(TOSMAKE_PATH)'))
$(eval include $(call TOSMake_find,$(1)))
endef

#  TOSMake_extra_targets(name)
#    Create a default make targets for a TOSMake extra full with its possible
#    options afterward.
define TOSMake_extra_targets
$(subst :,%,$(1)): FORCE
	@:
endef

#  TOSMake_include_dir(dir)
#    Pull in .extras and .targets from a directory which MUST be a subdir
#    under TOSMAKE_PATH.  Create default extra rules as necessary, etc.
TOSMake_include_dir = $(eval $(call TOSMake_include_dir_define,$(1)))
define TOSMake_include_dir_define
$(eval NEW_EXTRAS := $(call TOSMake_makelist,$(1),extra))
$(eval NEW_TARGETS := $(call TOSMake_makelist,$(1),target))
$(eval VALID_EXTRAS += $(NEW_EXTRAS))
$(eval VALID_TARGETS += $(NEW_TARGETS))
$(eval EXTRAS = $(filter $(call names,$(VALID_EXTRAS)),$(GOALS)))
$(eval TARGETS = $(filter $(call names,$(VALID_TARGETS)),$(GOALS)))
$(eval OTHERS = $(filter-out $(EXTRAS) $(TARGETS),$(GOALS)))
$(foreach file,$(NEW_EXTRAS) $(NEW_TARGETS),$(if $(filter $(call names,$(file)),$(GOALS)),$(eval include $(file))))
endef

TOSMake_accum_dir = $(eval $(call TOSMake_accum_dir_define,$(1)))
define TOSMake_accum_dir_define
$(eval NEW_EXTRAS := $(call TOSMake_makelist,$(1)/extras,extra))
$(eval NEW_TARGETS := $(call TOSMake_makelist,$(1)/targets,target))
$(eval VALID_EXTRAS += $(NEW_EXTRAS))
$(eval VALID_TARGETS += $(NEW_TARGETS))
$(eval TARGETS = $(filter $(call names,$(VALID_TARGETS)),$(GOALS)))
endef

# TOSMake_include_target(dir)
#   Pull in a .target file, which MUST be in the targets folder which is also a
#   subdir of TOSMAKE_PATH.
TOSMake_include_target=$(eval $(call TOSMake_include_target_define,$(1)))
define TOSMake_include_target_define
$(call TOSMake_include,targets/$(1).target)
endef

#  TOSMake_include_make_platform(dir)
#    Pull in a directory as a new TOSMake platform, which MUST be a subdir of
#    TOSMAKE_PATH.  A platform directory must also have a .rules file, which
#    is automatically evaluated.
TOSMake_include_make_platform=$(eval $(call TOSMake_include_make_platform_define,$(1)))
define TOSMake_include_make_platform_define
$(call TOSMake_include_dir,$(1))
$(call TOSMake_include,$(1)/$(1).rules)
TOSMAKE_MAKE_PLATFORM_DIR = $(shell dirname $(call TOSMake_find,$(1)/$(1).rules))
endef

# TOSMake_include_sensor(sensorboard)
#  Includes the [sensorboard].sensor file from the $TOSMAKE_PATH/sensors folder.
#  Use this function to add a sensorboard to a target.
TOSMake_include_sensor=$(eval $(call TOSMake_include_sensor_define,$(1)))
define TOSMake_include_sensor_define
$(call TOSMake_include,sensorboards/$(1).sensor)
PFLAGS += -DBOARD_$(shell echo $(1) | tr a-z A-Z)
endef

# TOSMake_include_platform(platform)
#  Use this to specify which platform the target is based on.
TOSMake_include_platform=$(eval $(call TOSMake_include_platform_define,$(1)))
define TOSMake_include_platform_define
$(if $(filter sim sim-fast sim-sf,$(GOALS)),$(call TOSMake_include,platforms/sim/$(1).platform),$(call TOSMake_include,platforms/$(1).platform))
PFLAGS += -DPLATFORM_$(shell echo $(1) | tr a-z A-Z)
PLATFORM = $(1)
endef

# TOSMake_include_family(family)
#  Specify that this target is part of a family.
TOSMake_include_family=$(eval $(call TOSMake_include_family_define,$(1)))
define TOSMake_include_family_define
$(call TOSMake_include,families/$(1).family)
endef


### ---
### --- Include Makelocal and Makedefaults
### ---

#  Makelocal comes first to allow overriding Makedefaults.
-include $(TINYOS_MAKELOCAL)
-include $(TINYOS_MAKEDEFAULTS)

#  Mark TOSMAKE_PATH with a + so that they're not searched for by TOSMake_find.
$(foreach incdir,$(addprefix +,$(TOSMAKE_PATH)),$(call TOSMake_accum_dir,$(incdir)))

$(foreach file,$(VALID_EXTRAS),$(if $(filter $(call names,$(file)),$(GOALS)),$(eval include $(file))))
$(foreach file,$(VALID_TARGETS),$(if $(filter $(call names,$(file)),$(GOALS)),$(eval include $(file))))

#  Make default rules for each extra with full argument
$(foreach goal,$(MAKECMDGOALS),$(if $(filter-out $(TARGETS) help,$(goal)),$(eval $(call TOSMake_extra_targets,$(goal)))))


# Check if $(COMPONENT) is set
ifeq ($(COMPONENT),)
$(error $(shell echo -e '$(ERROR_STRING) you must define the variable "COMPONENT" in the application makefile'))
endif


### ---
### --- Common targets
### ---

# Build storage file if volumes.xml present
ifneq ($(wildcard volumes-$(TOSMAKE_FLASH_CHIP).xml), )
build_storage: volumes-$(TOSMAKE_FLASH_CHIP).xml build_dir
	@echo -e '$(INFO_STRING) creating StorageVolumes.h'
	@tos-storage-$(TOSMAKE_FLASH_CHIP) $(TOSMAKE_VOLUME_ALLOCATOR_FLAGS) <volumes-$(TOSMAKE_FLASH_CHIP).xml > $(TOSMAKE_BUILD_DIR)/StorageVolumes.h || rm -f $(TOSMAKE_BUILD_DIR)/StorageVolumes.h

PFLAGS += -I$(TOSMAKE_BUILD_DIR)
else
build_storage:
endif

# Main build target for building an application
ifeq ($(TOSMAKE_DO_REINSTALL),)
tinyos_image: $(TOSMAKE_BINARY_FORMAT) $(TOSMAKE_POST_EXE_DEPS) build_tosimage build_buildinfo wiring_check FORCE
else
tinyos_image:
	@echo -e '$(INFO_STRING) reinstalling $(COMPONENT) binary'
endif

setid: tinyos_image FORCE
ifneq ($(NODEID),)
	@# Edit the binary with the updated nodeid
	@echo -e '$(INFO_STRING) setting the node id to $(NODEID)'
	@$(SET_ID) --objcopy $(OBJCOPY) --objdump $(OBJDUMP) --target $(TOSMAKE_BINARY_FORMAT) $(TOSMAKE_BINARY_IN) $(TOSMAKE_BINARY_OUT) $(TOSMAKE_TOS_NODE_ID_STR)=$(NODEID) $(TOSMAKE_AMADDR_STR)=$(NODEID)
else
	@# Just use the default NODEID that was compiled in initially
	@cp $(TOSMAKE_BINARY_IN) $(TOSMAKE_BINARY_OUT)
endif

delsetid: program FORCE
	rm -f $(subst .$(TOSMAKE_BINARY_FORMAT).,.exe.,$(TOSMAKE_BINARY_OUT)) $(TOSMAKE_BINARY_OUT)

ihex: bytes sym lst FORCE
	@echo -e '$(INFO_STRING) creating ihex file'
	@$(OBJCOPY) --output-target=ihex $(MAIN_EXE) $(MAIN_IHEX)

srec: bytes sym lst FORCE
	@echo -e '$(INFO_STRING) creating srec file'
	@$(OBJCOPY) --output-target=srec $(MAIN_EXE) $(MAIN_SREC)

bin: bytes sym lst FORCE
	@echo -e '$(INFO_STRING) creating bin file'
	@$(OBJCOPY) --output-target=binary $(MAIN_EXE) $(MAIN_BIN)

bytes: exe FORCE
	@echo -e '$(INFO_STRING) script'
	@$(OBJDUMP) -h $(MAIN_EXE) | perl -ne '$$b{$$1}=hex $$2 if /^\s*\d+\s*\.(text|data|bss)\s+(\S+)/; END { printf("%16d bytes in ROM\n%16d bytes in RAM\n",$$b{text}+$$b{data},$$b{data}+$$b{bss}); }'
	@echo -e '$(INFO_STRING) size (toolchain):'
	@$(SIZE) $(MAIN_EXE)

lst: exe FORCE
	@echo -e '$(INFO_STRING) generating listing'
	@$(OBJDUMP) -d --source -w $(MAIN_EXE) > $(MAIN_LST)

sym: exe FORCE
	@echo -e '$(INFO_STRING) generating symbol table'
	@$(NM) -an $(MAIN_EXE) > $(MAIN_SYM)

ifeq ($(NO_WIRING_CHECK),)
wiring_check: exe FORCE
	@echo -e '$(INFO_STRING) running the wiring check'
	@nescc-wiring $(WIRING_CHECK_FILE)
else
wiring_check:
endif

exe: $(TOSMAKE_PRE_EXE_DEPS) $(COMPONENT).nc build_dir build_storage ident_cache FORCE
	@echo -e '$(INFO_STRING) compiling $(COMPONENT) to a $(TARGET) binary'
	$(NESC) -o $(MAIN_EXE) $(TOSMAKE_FIRST_FLAGS) $(SAFE_FLAGS) $(OPTFLAGS) $(NESC_PFLAGS) $(CFLAGS) $(WIRING_CHECK_FLAGS) $(COMPONENT).nc $(LIBS) $(LDFLAGS) $(TOSMAKE_ADDITIONAL_INPUTS)

ident_cache: build_dir FORCE
	@echo '$(IDENT_FLAGS)' > $(TOSMAKE_BUILD_DIR)/ident_flags.txt

build_dir:
	@mkdir -p $(TOSMAKE_BUILD_DIR)




### ---
### --- Define USAGE, print help if necessary or requested, etc.
### ---

#  USAGE is printed out when help is requested.  Files other than this should
#  add text to HELP, not USAGE.
define USAGE


Usage:  make <target> <extras>
        make <target> help

        Valid targets: $(call names,$(VALID_TARGETS))
        Valid extras: $(call names,$(VALID_EXTRAS))
$(HELP)

endef

#  If no target or an invalid target is specified, print usage.
ifeq ($(TARGETS),)
  ifeq ($(GOALS),)
    $(error $(USAGE)$(shell echo -e '$(ERROR_STRING) Please specify a valid target'))
  else
    $(error $(USAGE)$(shell echo -e '$(ERROR_STRING) "$(GOALS)" does not specify a valid target'))
  endif
endif

#  If the user specifically had help on the command line, don't build any
#  targets, instead display help information and exit with a nice error.
ifeq ($(filter help,$(GOALS)),help)
define USAGE


Usage:  make $(TARGETS) <extras>

        Valid extras: $(call names,$(VALID_EXTRAS))
$(HELP)

endef
$(error $(USAGE))
endif

$(COMPONENT).nc:
	@echo -e '$(ERROR_STRING) You need to create a top level file called $(COMPONENT).nc, or modify your local Makefile to point to the real name of your top level component.'
	@false

.PHONY: FORCE

